package com.tre.jdevtemplateboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tre.jdevtemplateboot.common.context.BaseContext;
import com.tre.jdevtemplateboot.common.dto.PageQuery;
import com.tre.jdevtemplateboot.common.dto.ResponseResult;
import com.tre.jdevtemplateboot.domain.dto.SpAppGroupMstDto;
import com.tre.jdevtemplateboot.domain.po.SpAppGroupMst;
import com.tre.jdevtemplateboot.domain.vo.SpAppGroupMstVO;
import com.tre.jdevtemplateboot.mapper.SpAppGroupMstMapper;
import com.tre.jdevtemplateboot.service.ISpAppGroupMstService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JDev
 * @since 2023-11-15
 */
@Service
@Slf4j
public class SpAppGroupMstServiceImpl extends ServiceImpl<SpAppGroupMstMapper, SpAppGroupMst> implements ISpAppGroupMstService {

    @Autowired
    private SpAppGroupMstMapper spAppGroupMstMapper;

    @Override
    public ResponseResult<SpAppGroupMstVO> findGroupByPage(PageQuery query) {
        SpAppGroupMstVO vo = new SpAppGroupMstVO();
//        vo.setGroups(spAppGroupMstMapper.findGroupByPage(query.getPageSize(),query.getPageNum()));
        Page<SpAppGroupMst> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<SpAppGroupMst> p = lambdaQuery().page(page);
        vo.setGroups(p.getRecords());
        vo.setTotal(p.getTotal());
        return ResponseResult.buildOK(vo);
    }

    @Override
    public ResponseResult saveGroup(SpAppGroupMstDto dto) {
        log.info("新增分组：{}",dto);
        SpAppGroupMst po = new SpAppGroupMst();
        BeanUtils.copyProperties(dto,po);
        po.setCreateDate(LocalDateTime.now());
        po.setEditDate(LocalDateTime.now());
        po.setCreatePerson(BaseContext.getCurrentId());
        po.setEditPerson(BaseContext.getCurrentId());
        return ResponseResult.buildOK();
    }

    @Override
    public ResponseResult editGroup(SpAppGroupMstDto dto) {
        SpAppGroupMst po = new SpAppGroupMst();
        po.setId(dto.getId().getBytes());
        po.setName(dto.getName());
        po.setEditDate(LocalDateTime.now());
        po.setEditPerson(BaseContext.getCurrentId());
        updateById(po);
        return ResponseResult.buildOK();
    }
}
